﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft Corp." file="DeleteDeploymentCommand.cs">
//   All rights reserved. See License.txt in the project root for license information.
// </copyright>
// 
// --------------------------------------------------------------------------------------------------------------------
namespace AzureManagement.AzureCommands
{
    using System;

    using AzureManagement.AzureDataContracts;

    using RestClient;

    public class DeleteDeploymentCommand : AzureManagementCommand
    {
        private readonly string deploymentName;

        private readonly string serviceName;

        private readonly DeploymentSlots deploymentSlot;

        public DeleteDeploymentCommand(IAzureServiceContext serviceContext, string serviceName, string deploymentName)
            : base(serviceContext)
        {
            if (string.IsNullOrEmpty(deploymentName))
            {
                throw new ArgumentException("Deployment name must not be null or empty", "deploymentName");
            }

            if (string.IsNullOrEmpty(serviceName))
            {
                throw new ArgumentException("Service name must not be null or empty", "serviceName");
            }

            this.deploymentName = deploymentName;
            this.serviceName = serviceName;
        }

        public DeleteDeploymentCommand(
            IAzureServiceContext serviceContext, string serviceName, DeploymentSlots deploymentSlot)
            : base(serviceContext)
        {
            if (string.IsNullOrEmpty(serviceName))
            {
                throw new ArgumentException("Service name must not be null or empty", "serviceName");
            }

            this.deploymentSlot = deploymentSlot;
            this.serviceName = serviceName;
        }

        protected override RequestContext GetRequestContext()
        {
            return this.CreateDeleteOperationRequestContext();
        }

        protected override string FormatPath(IAzureServiceContext azureServiceContext)
        {
            if (string.IsNullOrEmpty(this.deploymentName))
            {
                return string.Format(
                            "{0}/services/hostedservices/{1}/deploymentslots/{2}", 
                            azureServiceContext.SubscriptionId, 
                            this.serviceName, 
                            this.deploymentSlot.ToString().ToLower());
            }

            return string.Format(
                        "{0}/services/hostedservices/{1}/deployments/{2}", 
                        azureServiceContext.SubscriptionId, 
                        this.serviceName, 
                        this.deploymentName);
        }
    }
}